package demo.practice.dp;

import java.util.HashMap;
import java.util.Map;

public class P523_2 {

    public static void main(String[] args) {
        P523_2 p523_2= new P523_2();
        boolean b = p523_2.checkSubarraySum(new int[]{0, 1, 0, 3, 0, 4, 0, 4, 0}, 5);
        System.out.println(b);
    }
    public boolean checkSubarraySum(int[] nums, int k) {

        if(nums==null|| nums.length<2)
            return false;
        //首先构造一个   前缀和
        int [] store = new int[nums.length+1] ;

        for(int i=0;i<nums.length;i++){
            store[i+1]= store[i]+ nums[i];
        }

        //store[1]= nums[0]
        //store[2]=nums[0]+ nums[1]
        Map<Integer,Integer> map= new HashMap<>();

        for(int i=1;i<nums.length+1 ;i++){
            int y= store[i]%k;
            if(y==0&&i>=2)
                return  true;
            if( map.get(y)==null){
                map.put(y,i);
            }else if( map.get(y) - i>=1){
                return true;
            }
        }
        return false;
    }
}
